package database.local;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import server.SaveZoneHttpConnection;
import server.XmlParser;


public class QueryData {
	/*days bitmap consts*/
	public static int SUNDAY = SaveZoneHttpConnection.SUNDAY;
	public static int MONDAY = SaveZoneHttpConnection.MONDAY;
	public static int TUESDAY = SaveZoneHttpConnection.TUESDAY;
	public static int WEDNESDAY = SaveZoneHttpConnection.WEDNESDAY;
	public static int THURSDAY = SaveZoneHttpConnection.THURSDAY;
	public static int FRIDAY = SaveZoneHttpConnection.FRIDAY;
	public static int SATURDAY = SaveZoneHttpConnection.SATURDAY;
	public static int ALL_DAYS = SaveZoneHttpConnection.ALL_DAYS;
	
	
	public static int REST = 0x1;
	public static int PUB = 0x2;
	public static int PARTY = 0x4;
	public static int ALL_TYPES = 0xff;
	
	private String country;
	private String city;
	private String street;
	private String time;
	private int days_bitmap;
	private int timeDelta;
	private List<Business> result;
	private Boolean changed;
	private String xml;
	private int type_btm;
	private String user_email;
	
	public QueryData(){
		setCountry(null);
		setCity(null);
		setStreet(null);
		setTime(null);
		setTimeDelta(0);
		changed = true;
		setData(new LinkedList<Business>());
		setType_btm(ALL_TYPES);
		setDays_bitmap(ALL_DAYS);
		setUser_email(null);
	}
	
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		changed = true;
		this.country = country;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		changed = true;
		this.city = city;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		changed = true;
		this.street = street;
	}
	public String getTime() {
		return time;
	}
	
	/** 
	 * @param time - should be a string "hh:mm"
	 */
	public void setTime(String time) {
		changed = true;
		this.time = time;
	}
	
	public int getTimeDelta() {
		return timeDelta;
	}
	
	public void setTimeDelta(int timeDelta) {
		changed = true;
		this.timeDelta = timeDelta;
	}
	
	public List<Business> getData() throws IOException {
		if (changed)  this.dealQuery();
		return result;
	}
	

	public String getUser_email() {
		return user_email;
	}

	public void setUser_email(String user_email) {
		this.user_email = user_email;
	}
	
	public List<Business> getUserData() {
		List<Business> business_lst = new ArrayList<Business>();
		if (getUser_email() != null) {
			Map<String, String> data = new HashMap<String, String>();
			data.put("email", getUser_email());
			InputStream stream_1 = SaveZoneHttpConnection.user_query(data);
			
			/*get result*/
			if (stream_1 != null) { 
				List<Business> temp = XmlParser.getBusinessList(stream_1);
				if (temp != null) {
					business_lst=temp;
				}
				try {
					stream_1.close();
				} catch (IOException e) {
					return business_lst;
				}
			}			
			return business_lst;
		}
		
		return business_lst;
	}
	
	public List<CheckIn> getCheckIns(Business business) {
		List<CheckIn> check_in_lst = new ArrayList<CheckIn>();
		
		if (business != null && business.getKey() != null && business.getKey() != "") {
			check_in_lst = SaveZoneHttpConnection.getCheckInList(business);
		}
		
		 return check_in_lst;
		
	}
	
	public Likes getUserLikes(User user) {
		return user.getLikeList();
	}

	/**
	 * getting from the results from the server according to setting params.
	 * in order to get results use this.getData.
	 * @return true if succeeded or false if failed
	 * @throws IOException 
	 * 
	 */
	private Boolean dealQuery() throws IOException {
		
		Map<String, String> data = new HashMap<String, String>();


		/*params*/
		data.put("type", Integer.toString(getType_btm()));
		if (getCountry() != null)
			data.put("country", getCountry());
		if (getCity() != null)
			data.put("city", getCity());
		if (getStreet() != null)
			data.put("street", getStreet());
		if (getTime() != null) {
			data.put("time", getTime().toString());
			data.put("time_delta", Integer.toString(getTimeDelta()));
		}
		data.put("days_bitmap", String.valueOf(getDays_bitmap()));
		
		InputStream stream_1 = SaveZoneHttpConnection.query(data);
		//InputStream stream_2 = SaveZoneHttpConnection.query(data);
	
		/*get result*/
		if (stream_1 != null) { 
			setData(XmlParser.getBusinessList(stream_1));
			stream_1.close();
		}
		
//		/*get xml*/
//		if (stream_2 != null) {
//			StringBuffer answer = new StringBuffer();
//			BufferedReader reader = new BufferedReader(new InputStreamReader(stream_2));
//			String line;
//			while ((line = reader.readLine()) != null) {
//				answer.append(line);
//			}
//			stream_2.close();
//			reader.close();
//			setXml(answer.toString());
//		}
//		
	  
		if (this.xml == null) return false;
		return true;
		
	}

	private void setData(List<Business> result) {
		this.result = result;
	}

	public String getXml() throws IOException {
		if (changed) dealQuery();
		return xml;
	}

	private void setXml(String xml) {
		this.xml = xml;
	}

	public int getType_btm() {
		return type_btm;
	}

	public void setType_btm(int type_btm) {
		this.type_btm = type_btm;
	}

	/**
	 * @return the days_bitmap
	 */
	public int getDays_bitmap() {
		return days_bitmap;
	}

	/**
	 * @param days_bitmap - create bitmap using bitmap consts  
	 */
	public void setDays_bitmap(int days_bitmap) {
		this.days_bitmap = days_bitmap & ALL_DAYS ;
	}


}
